home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / DB_CLIPP / 2614.ZIP / DEFAUL.ZIP / DEFAULT.TXT
Text File  |  1990-09-28  |  5KB  |  157 lines

  1. Written by Brad Choate                                 CServe ID: 75056,1247
  2.  
  3.  
  4.                            The DEFAULT command.
  5.  
  6.  
  7. #command DEFAULT <v1> WITH <x1> [, <vN> WITH <xN> ];
  8.       => IF((<v1>)=NIL,<v1>:=<x1>,NIL) [; IF((<vN>)=NIL,<vN>:=<xN>,NIL)]
  9.  
  10.  
  11. I've found this command useful for initializing variables that have
  12. no value already.
  13.  
  14.  
  15. The command
  16.  
  17.    DEFAULT <variable> WITH <value>
  18.  
  19. is processed as ...
  20.  
  21.    IF( <variable> = NIL , <variable> := <value> , NIL )
  22.  
  23.  
  24. I use this command for every function I write that uses parameters. 
  25. This way, I can define default assignments for the parameter
  26. variables.
  27.  
  28.  
  29. FUNCTION  ShowHeader(cString1,cString2)
  30.  
  31.    DEFAULT cString1 WITH "", cString2 WITH ""
  32.  
  33.    @  0, 0
  34.    @  0, 0 SAY cString1
  35.    @  0,79-LEN(cString2) SAY cString2
  36.  
  37. RETURN NIL
  38.  
  39.  
  40. In the above example, the function displays the first parameter in
  41. the upper-left corner of the screen.  The second parameter is
  42. displayed in the upper-right corner of the screen.  But... what
  43. happens if...
  44.  
  45.    ShowHeader("Program Name")
  46.  
  47.    ...and you *HAVEN'T* included the DEFAULT command?  Well, most
  48. likely, you'll get "Program Name" printed on the upper-left corner
  49. of the screen, and then your program will crash when it tries to
  50. get the length of cString2 since you cannot read the length of NIL.
  51.  
  52.    When the DEFAULT command *IS* included, the function call
  53.  
  54.    ShowHeader("Program Name")
  55.  
  56.    does this:  it displays "Program Name" on the upper-left corner
  57. of the screen, and the displays a "" in the upper-right corner of the
  58. screen.  cString2="" because of the default command.  Since cString2
  59. equalled NIL upon entry into the function, the DEFAULT command declared
  60. it as a null string since that was what it's default was declared as.
  61. cString1 was not changed from the passed value since cString1 did not
  62. equal NIL upon entry into the function.
  63.  
  64. The long way about doing this is ...
  65.  
  66. FUNCTION  ShowHeader(cString1, cString2)
  67.  
  68.    IF cString1=NIL
  69.       cString1=""
  70.    ENDIF
  71.  
  72.    IF cString2=NIL
  73.       cString2=""
  74.    ENDIF
  75.  
  76.    .
  77.    .  // rest of the function as shown above
  78.    .
  79.  
  80. RETURN NIL
  81.  
  82. Now this works fine... but it involves a lot more effort.  The
  83. preprocessor converts the DEFAULT command to the equivalent of
  84. the code shown above, so why go to all the trouble?  Let the
  85. preprocessor do it for you.
  86.  
  87.                             * * *
  88.  
  89. We must be careful not to create too many #commands, however.
  90. Dozens of new commands, sprinkled throughout a program makes it
  91. really hard to understand for others that don't know about the
  92. language extensions that the original programmer developed.
  93.  
  94. However, the DEFAULT command is better suited as a command, and
  95. not a function.  This is because it needs to have the syntax of the
  96. REPLACE command, the functionality of the STORE command, but the
  97. brains to know when to store the value to the variable and when not
  98. to.
  99.  
  100.  
  101. Here are some more examples of using the DEFAULT command.  These are
  102. just to show how it works, and are not really practical uses.
  103.  
  104.  
  105. cString2:="this is the second string"
  106. cString4:="this is the first string"
  107.  
  108. DEFAULT cString1 WITH "String", cString2 WITH "String",;
  109.         cString3 WITH "String", cString4 WITH "String"
  110.  
  111. ? cString1    //   String
  112. ? cString2    //   this is the second string
  113. ? cString3    //   String
  114. ? cString4    //   this is the first string
  115.  
  116.  
  117. As you can see from this example, DEFAULT only assigns values to
  118. variables that have no value already.  cString1 and cString3 had
  119. not been initialized, so DEFAULT initialized them with "String".
  120. cString2 and cString4 retained their values because they did not
  121. have NIL values.  They were already initialized and had a value.
  122.  
  123. By the way, the default values can be anything you want.  They do
  124. not have to be character strings.  And they do not have to be the same.
  125.  
  126. DEFAULT nDiscount WITH 10, cTerms WITH "NET 30", aItems WITH {},;
  127.         lDropShip WITH .F., aAddress WITH {}
  128.  
  129. ..., etc.  Here we have declarations for default data types of
  130. numeric, character, logical, and arrays.  You can also declare
  131. defaults for any other data type including dates, and even code
  132. blocks (but I wouldn't know why).
  133.  
  134. Well.  It's now 2:30am, and I need my sleep.  I'll upload this
  135. tonight and I hope you have fun with it.  It's my first command
  136. and hopefully I won't get too carried away myself with writing
  137. commands.  I've only had Clipper 5.0 for two days, so this is
  138. a pretty simple implementation of the #command directive, but
  139. it really does suit the purpose it is intended for.
  140.  
  141. Enjoy.
  142.  
  143. Brad Choate (75056,1247)
  144. Software Extensions
  145.  
  146. p.s.   if any of this doesn't make any sense or you think that I
  147.        haven't explained this enough or that it is explained
  148.        wrong, then please pardon me.  I'm very tired right now.
  149.        Send me a message via EasyPlex and I'll post an addendum
  150.        on the forum.
  151.  
  152. Software Extensions...
  153.  
  154. Specializing in Clipper programming for business (or other) applications.
  155. Call us today for information on customized programming... (901) 274-8463.
  156.    ask for Barbara Oliver.  Or mail your messages to CServe ID 75056,1247.
  157.